<template>
  <div id="app" v-bind:style="appDivStyle">
      <div class="context" ref="context">
           <div class="queryBox" ref="queryBox">
                <el-form :inline="true" :model="formQueryCondition"
                         ref="form" size="mini">
                    <el-form-item :label="$t('Definition.queryLabel.definitionName')"   :label-width="queryformLabelWidthLong">
                        <el-input v-model.trim="formQueryCondition.definitionName.val" class="queryItemStyle" clearable
                                  :placeholder="$t('Definition.queryLabel.placeholderDefinitionName')">
                        </el-input>
                    </el-form-item>
                    <el-form-item :label="$t('Definition.queryLabel.status')"  :label-width="queryformLabelWidthLong">
                        <el-select v-model="formQueryCondition.isEnable.val" class="queryItemStyle">
                            <el-option :label="$t('label.all')" value=""></el-option>
                            <el-option :value="drug.valueCode" :key="drug.valueCode" :label="drug.valueText" v-for="drug in enables"></el-option>
                            <!-- <el-option :label="$t('label.available')" value="Available"></el-option>
                            <el-option :label="$t('label.invalid')" value="Invalid"></el-option> -->
                        </el-select>
                    </el-form-item>


                    <div style="text-align:right">
                        <el-form-item>
                            <el-button v-permission="{menuCode:menuCode,resourceCode:'saveBtnText'}" type="primary" v-if="test('onNewBtn')"
                            @click="onNew('saveBtnText')">{{ $t('btnText.newBtnText') }}</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button v-permission="{menuCode:menuCode,resourceCode:'resetBtnText'}" type="primary" @click="onReset('form')">{{ $t('btnText.resetBtnText') }}</el-button>
                        </el-form-item>
                        <!-- <el-form-item>
                            <el-button type="primary" @click="onExport" icon="el-icon-download" :loading="loading">{{ $t('btnText.exportBtnText') }}</el-button>
                        </el-form-item> -->
                        <el-form-item>
                            <el-button v-permission="{menuCode:menuCode,resourceCode:'queryBtnText'}" type="primary" @click="onQuery('queryBtnText')" icon="el-icon-search" :loading="loading">{{ $t('btnText.queryBtnText') }}</el-button>
                        </el-form-item>
                    </div>
                </el-form>
            </div>
            <div class="dataBox" ref="dataBox">

                <transition enter-active-class="animated zoomIn" leave-active-class="animated rotateOut">
                    <el-table v-if="isInit" ref="singleTable"
                                :data="tableData"
                                :height="tableHeight"
                                @sort-change="sortChange"
                                v-loading="loading" size="mini"
                                style="width: 100%;" stripe=stripe border=border>
                        <el-table-column type="index"  width="50" fixed="left"
                                            :label="$t('table.xh')" align="center">
                        </el-table-column>
                        <el-table-column :label="$t('table.op')" fixed="left" min-width="120">
                            <template slot-scope="scope">
                                <el-button v-permission="{menuCode:menuCode,resourceCode:'editBtnText'}" size="gv" icon="el-icon-edit" :loading="loading"
                                @click="edit(scope.$index, scope.row,'editBtnText')"
                                            >{{ $t('btnText.editBtnText') }}</el-button>
                                <!-- <el-button size="gv" icon="el-icon-delete"
                                            type="danger" :loading="loading" @click="deleteRow(scope.$index, scope.row)"
                                            >{{ $t('btnText.deleteBtnText') }}</el-button> -->
                                <!-- <el-button v-permission="{menuCode:menuCode,resourceCode:'passBtnText'}" size="gv" icon="el-icon-delete"
                                            type="danger" :loading="loading" @click="logoffRow(scope.$index, scope.row)" :disabled="scope.row.isEnable=='不可用'" 
                                            >{{ $t('btnText.logoffBtnText') }}</el-button> -->
                                            <el-button size="gv" icon="el-icon-delete" v-show="scope.row.isEnable=='Invalid'"
                                            v-permission="{menuCode:menuCode,resourceCode:'delete'}"
                                            type="danger" :loading="loading" @click="deleteRow(scope.$index, scope.row,'delete')"
                                            >{{ $t('btnText.deleteBtnText') }}</el-button>
                                <el-button size="gv" icon="el-icon-delete" v-show="scope.row.isEnable=='Available'" v-permission="{menuCode:menuCode,resourceCode:'passBtnText'}"
                                            type="danger" :loading="loading" @click="logoffRow(scope.$index, scope.row,'passBtnText')"
                                            >{{ $t('btnText.logoffBtnText') }}</el-button>
                            </template>
                        </el-table-column>
                        <el-table-column prop="definitionCode" min-width="140" fixed="left"
                                            :label="$t('Definition.table.definitionCode')" sortable="custom" sort-by="definitionCode">
                        </el-table-column>
                        <el-table-column prop="definitionName" min-width="140" fixed="left"
                                            :label="$t('Definition.table.definitionName')" sortable="custom" sort-by="definitionName">
                        </el-table-column>
                        <el-table-column prop="definitionDesc" min-width="140"
                                            :label="$t('Definition.table.desc')" sortable="custom" sort-by="definitionDesc">
                        </el-table-column>
                        <el-table-column prop="isEnableName" min-width="110" sortable="custom"
                        :label="$t('Definition.table.status')" sort-by="isEnable">
                        </el-table-column>

                        <el-table-column :label="$t('Definition.table.lastUpdatedBy')" min-width="120" sortable="custom" prop="modifier" sort-by="modifier">
                        </el-table-column>
                        <el-table-column :label="$t('Definition.table.lastUpdateTime')" min-width="180" sortable="custom" prop="lastUpdatedDate" sort-by="lastUpdatedDate">
                        </el-table-column>

                    </el-table>
                </transition>

            </div>
        </div>
        <div class="pageBox">
            <el-pagination @size-change="sizeChange"
                            @current-change="currentPageChange"
                            :current-page="pager.curPage"
                            :page-sizes="pager.sizes"
                            :page-size="pager.curSize"
                            :background="true"
                            layout="total, sizes, prev, pager, next, jumper" :total="pager.total">
            </el-pagination>
        </div>

         <!--编辑对话框-->
        <transition enter-active-class="animated rotateIn" leave-active-class="animated rotateOut">
            <el-dialog :title="$t('label.dialogTitle')" :visible.sync="isShowEditForm" top="5vh">
                <el-form :model="editForm" ref="editForm" size="mini"
                        :label-position="editFormPosition" status-icon=status-icon :rules="editRules">
                    <el-input v-model.trim="editForm.keyId"  v-show="false"></el-input>
                    <el-form-item :label="$t('Definition.editLabel.definitionCode')"
                        :label-width="editformLabelWidth" prop="definitionCode">
                        <el-input v-model.trim="editForm.definitionCode" maxlength="20" :disabled="canEditCode"
                                :placeholder="$t('Definition.editLabel.placeholderDefinitionCode')"></el-input>
                    </el-form-item>
                    <el-form-item :label="$t('Definition.editLabel.definitionName')"
                        :label-width="editformLabelWidth" prop="definitionName">
                        <el-input v-model.trim="editForm.definitionName" maxlength="50"
                                :placeholder="$t('Definition.editLabel.placeholderDefinitionName')"></el-input>
                    </el-form-item>

                    <el-form-item :label="$t('Definition.editLabel.desc')"
                        :label-width="editformLabelWidth" prop="definitionDesc">
                        <el-input v-model="editForm.definitionDesc" maxlength="100" type="textarea"
                                :placeholder="$t('Definition.editLabel.placeholderDesc')"></el-input>
                    </el-form-item>
                     <el-form-item :label="$t('Definition.editLabel.status')"
                        :label-width="editformLabelWidth" prop="isEnable">
                        <el-select v-model="editForm.isEnable">
                            <el-option :value="drug.valueCode" :key="drug.valueCode" :label="drug.valueText" v-for="drug in enables"></el-option>
                            <!-- <el-option :label="$t('label.available')" value="Available"></el-option>
                            <el-option :label="$t('label.invalid')" value="Invalid"></el-option> -->
                        </el-select>
                    </el-form-item>
                </el-form>
                <div slot="footer">
                    <el-button size="mini" @click="isShowEditForm = false" :loading="loading">{{ $t('btnText.cancelBtnText') }}</el-button>
                    <el-button size="mini" type="primary" @click="submitForm('editForm',currentResCode)" :loading="loading">{{ $t('btnText.saveBtnText') }}</el-button>
                </div>
            </el-dialog>
        </transition>


  </div>


</template>

<script>

import comBaseVue from '../../baseViewModel';

export default {
  mixins: [comBaseVue],
  name:'Definition',
  props:{
    menuCode:String
  },
  data () {

    return {
        queryformLabelWidthLong:"110px",
        isInit:false,
        tableHeight:100,
        enables: [],
        appDivStyle:{
            height:this.$store.getters.appHeight,
        },
        modelName:"com.ispr.iptv.entity.sys.SysDefinition",
        // 查询条件
        formQueryCondition: {
            definitionName: {
              //mapName: 'code.key',  映射的别名 在同时查询一个属性时需要别名
              //gName:'code',   组名
              key:'definitionName',
              op: 'LIKE', // 操作符
              val: '' // 查询的值
            },
            isEnable: {
              op: '=', // 操作符
              key:'isEnable',
              val: ''
            },
        },

        // 编辑面板绑定的对象
        editForm: {
            keyId: 0,
            version: 0,
            definitionCode: "",
            definitionName: "",
            definitionDesc: "",
            isEnable: "",
        },
        // 编辑面板的验证规则
        editRules: {
            definitionCode: [{ required: true, message: this.$i18n.t('Definition.rules.placeholderDefinitionCode'), trigger: 'blur' }],
            definitionName: [{ required: true, message: this.$i18n.t('Definition.rules.placeholderDefinitionName'), trigger: 'blur' }],
            // desc: [{ required: false, message: this.$i18n.t('Definition.rules.placeholderDesc'), trigger: 'blur' }],
        },
    }
  },
  methods:{
      test:function(btnCode){
          return true;
        },
    // 新增
    onNew: function(rescode) {
        this.currentResCode = (rescode && rescode!=='') ? rescode : '';
        this.isShowEditForm = true;
        if (this.$refs['editForm']) {
            this.$refs['editForm'].resetFields();
        }
        this.editForm.keyId = "";
        this.editForm.version = "";
        this.editForm.definitionCode = "";
        this.editForm.definitionName = "";
        this.editForm.definitionDesc = "";
        this.editForm.isEnable = "Available";
        this.curCommand = "SaveNewReturnPK";
        this.canEditCode = false;
    },

     // 重置
    onReset:function(){
        if (this.$refs['form']) {
            this.$refs['form'].resetFields();
        }
        this.formQueryCondition.definitionName.val = "";
        this.formQueryCondition.isEnable.val = "Available";
    },

    // 编辑按钮回调事件
    editCallback: function(isSuccess,json) {
        if(isSuccess){
            this.curCommand = "PartialUpdateProc";
            this.isShowEditForm = true;
            if (this.$refs['editForm']) {
                this.$refs['editForm'].resetFields();
            }
            this.editForm.definitionCode = json.definitionCode;
            this.editForm.definitionName = json.definitionName;
            this.editForm.definitionDesc = json.definitionDesc;
            this.editForm.isEnable = json.isEnable;
            this.editForm.version = json.version;
            this.editForm.keyId = json.keyId;
            this.canEditCode = true;
        }
        else{
            this.$message({
                message: json.msg,
                type: 'warning'
                });
        }
    },

// 为状态下拉框赋值
    findEnables: function() {
        this.findByTypeCode("10001",this.setEnables)//根据类型编码去找需要的值
    },
    // 查询状态下拉框数据成功后的回调
    setEnables: function(isSuccess,json) {
        try{
            if (isSuccess) {
                this.enables = [];
                for (var i = 0; i < json.data.length; i++) {
                    this.enables.push(json.data[i]);
                }
            }
            else {
                this.$message.error(json.msg);
            }
        }
        catch(e){
            alert(e);
        }
    },

    initTable:function(){
        this.tableHeight = this.$refs.context.offsetHeight - this.$refs.queryBox.offsetHeight;
        this.isInit = true;
    },


  },
  mounted: function mounted() {
    this.formQueryCondition.isEnable.val = "Available";
    this.findEnables();
    this.onQuery();
    window.setTimeout(this.initTable,100);
  }
}


</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
